'use strict';
app.controller('ClientCtrl', ['$scope', '$modal', '$rootScope', '$state', '$filter', 'toaster', 'commonService', '$http', 'userService', function ($scope, $modal, $rootScope, $state, $filter, toaster, commonService, userService, $http) {
    $scope.page_code = "client";
    $scope.matchString = "";
    function getTbDefinitionsFromServer() {
        return {
            "serverSide": true,
            "ajax": {
                url: "client/plist", data: function (d) {
                    d.matchString = $scope.matchString;
                    for (var key in d) {
                        if (key.indexOf("columns") == 0) {
                            delete d[key];
                        }
                    }
                }
            },
            aoColumns: [{
                mData: 'id',
                sClass: "center"
            }, {mData: 'name'}, {mData: 'creditCode'}, {mData: 'legalPerson'},
                {mData: 'capital'}, {mData: 'doneDate'},
                {mData: 'industryName'}, {mData: 'contactor'}, {mData: 'phone'},

            ],
            aoColumnDefs: [{
                aTargets: [0], mRender: function (data, type, full) {
                    var editId = "client" + data;
                    return '<label class="i-checks m-b-none"><input id = "' + editId + '" class="selectListCmPara" type="checkbox"><i></i></label>';
                }, bSortable: false

            }, commonService.tableRowAllowNullNoSort(1),
                commonService.tableRowAllowNullNoSort(2),
                commonService.tableRowAllowNullNoSort(3),
                commonService.tableRowAllowNullNoSort(4),
                commonService.tableRowAllowNullNoSort(5),
                commonService.tableRowAllowNullNoSort(6),
                commonService.tableRowAllowNullNoSort(7),
                commonService.tableRowAllowNullNoSort(8)],
            fnDrawCallback: function (oSettings, json) {
                $(".selectListCmPara").on('click', function () {
                    $(".selectListCmPara").not(this).attr("checked", false);
                });
            }
        };
    };
    $scope.tbOptions = getTbDefinitionsFromServer();

    $scope.add = function () {
        var param = {type: 'create'};
        commonService.openEditDialog(param, 'edit', 'ClientEditCtrl');
    };
    $scope.edit = function () {
        var info = commonService.getSelectData("clientTable", "client", "id");
        if (info != null) {
            var param = {type: 'edit', info: info};
            commonService.openEditDialog(param, 'edit', 'ClientEditCtrl');
        } else {
            toaster.pop('info', '提示', '请选择需要修改的信息。');
        }
    };
    $scope.delete = function () {
        var info = commonService.getSelectData("clientTable", "client", "id");
        if (info != null) {
            BootstrapDialog.confirm("确认要删除企业：" + info.name, function (result) {
                if (result) {
                    commonService.httpJson("client/ditem", {id: info.id}).success(function (data, status) {
                        if (data && data.code == 0) {
                            toaster.pop('info', '提示', data['message']);
                            $scope.refresh();
                        } else {
                            toaster.pop("error", "错误", data['message']);
                        }
                    });
                }
            });
        } else {
            toaster.pop('info', '提示', '请选择需要修改的信息。');
        }
    };
    $scope.refresh = function () {
        var table = $('#clientTable').DataTable();
        table.ajax.reload();
    };
    $scope.search = function () {
        $scope.refresh();
    };
    $scope.$on('client.update', function (event) {
        $scope.refresh();
    });
}]);
app.controller('ClientEditCtrl',
    ['$scope',
        '$rootScope',
        '$modalInstance',
        'param',
        'toaster',
        'commonService',
        'userService', '$http',
        function ($scope,
                  $rootScope,
                  $modalInstance,
                  param,
                  toaster,
                  commonService,
                  userService,
                  $http) {
            $scope.client = {};

            $scope.provices = [];
            $scope.citys = [];
            $scope.countrys = [];

            getProvices();
            function getProvices() {
                commonService.httpJson("proviceRef/drop", {})
                    .success(function (data) {
                        if (data && data.code == 0) {
                            $scope.provices = data['data'];
                        } else {
                            commonService.toasterError(data['message']);
                        }
                    });
            }

            function getCitys() {
                commonService.httpJson("cityRef/drop", {proviceId: $scope.client.proviceId})
                    .success(function (data) {
                        if (data && data.code == 0) {
                            $scope.citys = data['data'];
                        } else {
                            commonService.toasterError(data['message']);
                        }
                    });
            }

            function getCountry() {
                commonService.httpJson("countryRef/drop", {cityId: $scope.client.cityId})
                    .success(function (data) {
                        if (data && data.code == 0) {
                            $scope.countrys = data['data'];
                        } else {
                            commonService.toasterError(data['message']);
                        }
                    });
            }


            $scope.$watch('client.proviceId', function (newValue) {
                //console.log($scope.client);
                //console.log(newValue)
                if (newValue) {
                    $scope.countrys = [];
                    getCitys();

                }
            });

            $scope.$watch('client.cityId', function (newValue) {
                // console.log($scope.client);
                // console.log(newValue)
                if (newValue) {
                    getCountry();
                }
            });

            $scope.$watch($scope.client)

            getIndustry();
            function getIndustry() {
                commonService.httpJson("industryType/drop", {})
                    .success(function (data) {
                        if (data && data.code == 0) {
                            $scope.industrys = data['data'];
                        } else {
                            commonService.toasterError(data['message']);
                        }
                    });
            }

            if (param.type == "edit") {
                $scope.client = param.info;
                $scope.client.updateUser = userService.getId();
            } else if (param.type = "create"){
                $scope.client.createUser = userService.getId();
            }

            $scope.submit = function () {
                if (checkSubmit()) {
                    commonService.httpJson("client/save", $scope.client)
                        .success(function (data) {
                            if (data && data.code == 0) {
                                commonService.editData("client.update", param.type);
                                toaster.pop("info", "信息", data['message']);
                                $modalInstance.close();
                            } else {
                                commonService.toasterError(data['message']);
                            }
                        });
                }
            }

            function checkSubmit() {
                var result = true;

                if (!$scope.client.name) {
                    commonService.toasterError("单位名称不能为空。");
                    result = false;
                }

                return result;
            }

            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }]);